//
//  SWEventFavoriteViewController.swift
//  ChildStory
//
//  Created by linhan on 2016/12/14.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWEventFavoriteViewController: SWBaseViewController,UITableViewDataSource,UITableViewDelegate,SWAPIComponent
{
    
    private var _list:[SWKidEvent] = []
    
    lazy private var _tableView:UITableView = UITableView()
    
    deinit
    {
        NotificationCenter.default.removeObserver(self)
        trace("DEINIT SWEventFavoriteViewController")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    private func setup()
    {
        title = "事件收藏"
        
        _tableView.frame = view.bounds
        _tableView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
        _tableView.backgroundColor = UIColor(hex:0xefefef)
        _tableView.separatorStyle = .none
        _tableView.tableFooterView = UIView()
        _tableView.dataSource = self
        _tableView.delegate = self
        view.addSubview(_tableView)
        
        NotificationCenter.default.addObserver(self, selector: #selector(dislikeKidEventNotification(_:)), name: NotificationName.DislikeKidEvent, object: nil)
        
        loadData()
    }
    
    private func loadData()
    {
        let req = SWFavoriteEventsFetchReq()
        req.successCallback = {[weak self] response,cache in
            if let response = response as? SWFavoriteEventsFetchRsp,
                let events:[SWKidEvent] = response.events?.flatMap({SWKidEvent(response: $0)})
            {
                self?._list = events
                self?._tableView.reloadData()
            }
        }
        load(req)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return _list.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        var height:CGFloat = SWKidEventCell.getCellHeight(cellWidth: view.width)
        //最后一个不需要分割
        if _list.count > 1 && indexPath.row < _list.count - 1
        {
            
            let CellGrid:CGFloat = 8
            height += CellGrid
        }
        return height
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let index:Int = indexPath.row
        var cell:UITableViewCell?
        let identifier:String = "SWKidEventCell"
        cell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if cell == nil
        {
            cell = SWKidEventCell(style: .default, reuseIdentifier: identifier, cellWidth: view.width)
        }
        (cell as? SWKidEventCell)?.event = _list.valueAt(index)
        return cell ?? UITableViewCell(style: .default, reuseIdentifier: "")
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: false)
        if let event = _list.valueAt(indexPath.row)
        {
            let viewController = SWEventDetailViewController()
            viewController.eventID = event.eventID ?? ""
            navigationController?.pushViewController(viewController, animated: true)
        }
    }
    
    @objc private func dislikeKidEventNotification(_ notification:Notification)
    {
        if let eventID = notification.userInfo?[SWKey.eventID] as? String,
           let index = _list.index(where: {($0.eventID ?? "") == eventID})
        {
            _list.removeAtIndexSafely(index)
        }
    }
    
    
}
